package com.action.Device;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.entity.Device;
import com.entity.Parameter;
import com.entity.Substation;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.service.DeviceService;
import com.service.ParameterService;
import com.service.SubstationService;
import com.util.JsonUtil;

public class DeviceAction extends ActionSupport implements SessionAware {
	private DeviceService deviceService;
	private ParameterService parameterService;
	private SubstationService substationService;
	private int deviceId;
	private String deviceNo;
	private int optype;
	private String deviceType;
	private Device device;
	private String deviceNorm;
	private Map session;
	private JSON indexSearchJSON;
	private String result;
	private String unitNo;
	private String substationNo;
	
	
	public void setSubstationService(SubstationService substationService) {
		this.substationService = substationService;
	}

	public void setParameterService(ParameterService parameterService) {
		this.parameterService = parameterService;
	}

	public void setDeviceService(DeviceService deviceService) {
		this.deviceService = deviceService;
	}
	
	public JSON getIndexSearchJSON() {
		return indexSearchJSON;
	}

	public void setIndexSearchJSON(JSON indexSearchJSON) {
		this.indexSearchJSON = indexSearchJSON;
	}

	public String getDeviceType() {
		return deviceType;
	}

	public void setDeviceType(String deviceType) {
		this.deviceType = deviceType;
	}

	public Device getDevice() {
		return device;
	}

	public void setDevice(Device device) {
		this.device = device;
	}

	public Map getSession() {
        return session;
    }

    public void setSession(Map session) {
        this.session = session;
    }

	public int getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(int deviceId) {
		this.deviceId = deviceId;
	}

	public int getOptype() {
		return optype;
	}

	public void setOptype(int optype) {
		this.optype = optype;
	}

	public String getDeviceNorm() {
		return deviceNorm;
	}

	public void setDeviceNorm(String deviceNorm) {
		this.deviceNorm = deviceNorm;
	}

	public String getDeviceNo() {
		return deviceNo;
	}

	public void setDeviceNo(String deviceNo) {
		this.deviceNo = deviceNo;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getUnitNo() {
		return unitNo;
	}

	public void setUnitNo(String unitNo) {
		this.unitNo = unitNo;
	}

	public String getSubstationNo() {
		return substationNo;
	}

	public void setSubstationNo(String substationNo) {
		this.substationNo = substationNo;
	}

	public String initAddDevice(){
		List<Parameter> Parameterlist=new ArrayList<Parameter>();
		Parameterlist = parameterService.findAllDeviceType();
		List<Substation> Substationlist=new ArrayList<Substation>();
		Substationlist = substationService.findAllSubstationByAvailable();
		session.put("deviceTypeList" , Parameterlist);
		session.put("substationList" , Substationlist);
		return SUCCESS;
	}
    
	
	public String addDeviceMethod(){
		if(deviceService.addDevice(device))
			return SUCCESS;
		else return INPUT;
	}
	
	public String findAllDevice() throws Exception{
		List<Device> Devicelist=new ArrayList<Device>();
		Devicelist = deviceService.findAllDevice();
		Map session = ActionContext.getContext().getSession();
		session.put("devicelist" , Devicelist);
		return SUCCESS;
	}
	
	public String findDeviceByTypeAndNorm() throws Exception{
		List<Device> Devicelist=new ArrayList<Device>();
		Devicelist = deviceService.findDeviceByTypeAndNorm(deviceType, deviceNorm);
		Map session = ActionContext.getContext().getSession();
		session.put("devicelist" , Devicelist);
		return SUCCESS;
	}
	
	public String executeEditDeviceStatus(){
		deviceService.editDeviceStatus(this.deviceId, this.optype);
		return SUCCESS;
	}
	
	public String findAuditingDevice(){
		List<Device> Devicelist=new ArrayList<Device>();
		Devicelist = deviceService.findAuditingDevice();
		session.put("devicelist", Devicelist);
		return SUCCESS;
	}
	
	public String checkDeviceNoAvailable(){
		if(deviceService.checkDeviceNoAvailable(deviceNo))
			result = "true";
		else result = "false";
		return SUCCESS;
	}
	
	public String deviceDetails(){
		List<Device> Devicelist=new ArrayList<Device>();
		Devicelist = deviceService.deviceDetails(unitNo,substationNo);
		session.put("devicelist", Devicelist);
		return SUCCESS;
	}
	
	
	public String deleteDevice(){
		deviceService.deleteDevice(deviceId);
		List<Device> Devicelist=new ArrayList<Device>();
		Devicelist = deviceService.findAllDevice();
		session.put("devicelist", Devicelist);
		return SUCCESS;
	}
	
	public String deleteDeviceByType(){
		deviceService.deleteDevice(deviceId);
		List<Device> Devicelist=new ArrayList<Device>();
		Devicelist = deviceService.findDeviceByTypeAndNorm(deviceType, deviceNorm);
		session.put("devicelist", Devicelist);
		return SUCCESS;
	}
}
